import { createRouter, createWebHistory } from 'vue-router'

// 定义动态菜单路由
export const menuRoutes = [
  {
    path: '/home', // 二级路由
    component: () => import('@/views/home/Index.vue'),
    meta: {
      title: '系统首页'
    }
  },
  {
    path: '/account',// 二级路由
    component: () => import('@/views/account/Index.vue'),
    redirect: '/account/adminlist',
    meta: {
      title: '账户管理'
    },
    children: [
      {
        path: '/account/adminlist', // 三级路由
        name: 'adminlist',
        component: () => import('@/views/account/Adminlist.vue'),
        meta: {
          title: '管理员列表'
        },
      }
    ]
  },
  {
    path: '/banner', // 二级路由
    component: () => import('@/views/banner/Index.vue'),
    redirect: '/banner/bannerlist',
    meta: {
      title: '轮播图管理'
    },
    children: [
      {
        path: '/banner/bannerlist', // 三级路由
        name: 'bannerlist',
        component: () => import('@/views/banner/Bannerlist.vue'),
        meta: {
          title: '轮播图列表'
        },
      },
      {
        path: '/banner/addbanner', // 三级路由
        name: 'addbanner',
        component: () => import('@/views/banner/Addbanner.vue')
      }
    ]
  },
  {
    path: '/pro', // 二级路由
    component: () => import('@/views/pro/Index.vue'),
    redirect: '/pro/prolist',
    meta: {
      title: '产品管理'
    },
    children: [
      {
        path: '/pro/prolist', // 三级路由
        name: 'prolist',
        component: () => import('@/views/pro/Prolist.vue'),
        meta: {
          title: '产品列表'
        },
      },
      {
        path: '/pro/filterlist', // 三级路由
        name: 'filterlist',
        component: () => import('@/views/pro/Filterlist.vue'),
        meta: {
          title: '筛选列表'
        },
      },
      {
        path: '/pro/seckilllist', // 三级路由
        name: 'seckilllist',
        component: () => import('@/views/pro/Seckilllist.vue'),
        meta: {
          title: '秒杀列表'
        },
      },
      {
        path: '/pro/recommendlist', // 三级路由
        name: 'recommendlist',
        component: () => import('@/views/pro/Recommendlist.vue'),
        meta: {
          title: '推荐列表'
        },
      }
    ]
  },
  {
    path: '/map',
    meta: {
      title: '地图管理'
    },
    component: () => import('@/views/map/Index.vue')
  },
  {
    path: '/editor',
    meta: {
      title: '富文本编辑器'
    },
    component: () => import('@/views/editor/Index.vue')
  },
  {
    path: '/setting',
    meta: {
      title: '系统设置'
    },
    component: () => import('@/views/setting/Index.vue')
  }
]
// 定义固定路由数组
const constantRoutes = [
  {
    path: '/login', // 一级路由
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/',  // 一级路由
    component: () => import('@/views/layout/Index.vue'),
    redirect: '/home', // 重定向
    children: menuRoutes  // 菜单路由
  },
  // 没有权限的路由
  {
    path: '/nopermission',
    name: 'nopermission',
    component: () => import('@/views/layout/Index.vue'),
    children: [
      {
        path: '/nopermission',
        component: () => import('@/views/nopermission/Index.vue'),
      }
    ]
  }

]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes
})


// 设置路由前置守卫
// next 参数在router@3中必须有
// next 参数在router@4中 可选
router.beforeEach((to, from) => {
  // to 表示下一个目标路由对象
  // from 表示上一个路由对象
  // 该函数没有 return 或return true  放行
  // return  false 中断导航
  // return {path:'url'} 重定向

  let token = localStorage.getItem('token');
  // 先判断先判断用户要跳转的路径
  if (to.path == '/login') {
    if (token) {
      return '/home'
    }
  } else {
    if (!token) {
      return { path: '/login' }
    }
  }
})


export default router
